import React from "react";
import store from "../../store/index";
class counter1 extends React.Component {
  state = {
    count: 0,
  };
  unsubscribe = () => {};
  componentDidMount() {
    // 组件挂载后立即调用
    store.subscribe(() => {
      this.setState({
        count: store.getState().count,
      });
    });
  }
  componentWillUnmount() {
    // 当组件从dom中移除调用
    this.unsubscribe();
  }
  render() {
    return (
      <div>
        <p>{this.state.count}</p>
        <button
          onClick={() => {
            store.dispatch({ type: "ADD" });
          }}
        >
          +
        </button>
        <button
          onClick={() => {
            store.dispatch({ type: "Minus" });
          }}
        >
          -
        </button>
      </div>
    );
  }
}

export default counter1;
